package com.hrhx.springboot.web;

import java.io.IOException;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hrhx.springboot.domain.IdCard;
import com.hrhx.springboot.domain.IndentityInfoBean;
import com.hrhx.springboot.service.IdCardService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/idcard")
public class IdCardController {
	
	@Autowired
	private IdCardService idCardService;
	
	@ApiOperation(value = "根据身份证码获取信息", notes = "")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "idCard", value = "320303198209095933", required = false, dataType = "IdCard")
	})
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	public IndentityInfoBean getByParentId(@ModelAttribute IdCard idCard) throws IOException {
		IndentityInfoBean indentityInfo = new IndentityInfoBean();
		if (idCardService.validateCard(idCard.getCardId())) {
			short year = idCardService.getYearByIdCard(idCard.getCardId());
			short month = idCardService.getMonthByIdCard(idCard.getCardId());
			short day = idCardService.getDateByIdCard(idCard.getCardId());
			indentityInfo.setVerify(idCardService.validateCard(idCard.getCardId()));
			indentityInfo.setBirthDate(idCardService.getBirthByIdCard(idCard.getCardId()));
			indentityInfo.setAge(idCardService.getAgeByIdCard(idCard.getCardId()));
			indentityInfo.setProvince(idCardService.getProvinceByIdCard(idCard.getCardId()));
			indentityInfo.setAddressDetail(idCardService.getCityByIdCard(idCard.getCardId()));
			indentityInfo.setBirthYear(year);
			indentityInfo.setBirthMonth(month);
			indentityInfo.setBirthDay(day);
			indentityInfo.setSex(idCardService.getGenderByIdCard(idCard.getCardId()));
			indentityInfo.setConstellation(idCardService.getConstellation(month, day));
			Calendar c = Calendar.getInstance();
			c.set(year, month, day);
			indentityInfo.setZodiac(idCardService.date2Zodica(c));	
		} else {
			indentityInfo.setVerify(false);
		}
		return indentityInfo;
	}
}
